package com.pangus.ims.mes.service;

import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.pangus.PangusConstants;
import com.pangus.common.dto.EditDataResult;
import com.pangus.common.dto.EditResult;
import com.pangus.common.dto.JqGridRequest;
import com.pangus.common.dto.JqGridResponse;
import com.pangus.common.dto.JsonRequest;
import com.pangus.common.excel.CheckExcelResult;
import com.pangus.common.excel.callback.ExcelCallbackCommonService;
import com.pangus.common.sys.dto.domain.LogDto;
import com.pangus.common.sys.service.ParameterService;
import com.pangus.common.sys.utils.CheckUtils;
import com.pangus.common.sys.utils.CommonUtils;
import com.pangus.common.sys.utils.MsgUtils;
import com.pangus.common.sys.utils.ParamUtils;
import com.pangus.common.sys.utils.PartnerCodeHolder;
import com.pangus.common.sys.utils.SqlQueryUtils;
import com.pangus.fa.i18n.FaMessageSource;
import com.pangus.fa.mybatis.mapper.entity.Condition.Criteria;
import com.pangus.fa.service.BaseService;
import com.pangus.fa.util.FastIdUtils;
import com.pangus.ims.bs.form.BsMnLpnForm;
import com.pangus.ims.mes.dto.domain.MesMoCustDriverLogDto;
import com.pangus.ims.mes.form.MesMoCustDriverLogForm;
import com.pangus.ims.mes.form.MesMoCustExchConfigForm;
import com.pangus.fa.mybatis.mapper.entity.Condition;

@Service
public class MesMoCustDriverLogService extends BaseService<MesMoCustDriverLogDto> implements ExcelCallbackCommonService<MesMoCustDriverLogForm>{
	
	@Autowired
	private SqlQueryUtils sqlQueryUtils;
	
	@Autowired
	private FaMessageSource messageSource;
	
	@Autowired
    private ParameterService parameterService;
	
	@Autowired
    private FastIdUtils fastIdUtils;
	
	/**
	*查询本表sql
	*/
	public String getQuerySql() {
		String sql = "SELECT b.mes_driver_log_id,b.partner_code,b.mo_id,b.lb_id,b.bs_cust_exch_config_id,b.driver_result,"
				+ "b.created_by,b.created_date,b.updated_by,b.updated_date,"
				+ "b.mes_mo_cust_driver_log_ex1,b.mes_mo_cust_driver_log_ex2,b.mes_mo_cust_driver_log_ex3,"
				+ "b.mes_mo_cust_driver_log_ex4,b.mes_mo_cust_driver_log_ex5,b.mes_mo_cust_driver_log_ex6,"
				+ "b.mes_mo_cust_driver_log_ex7,b.mes_mo_cust_driver_log_ex8,b.mes_mo_cust_driver_log_ex9"
				+ " FROM mes_mo_cust_driver_log b";
		return sql;
	}
	
	@Override
	public JqGridResponse<MesMoCustDriverLogForm> query(JqGridRequest<MesMoCustDriverLogForm> request,ParamUtils paramUtils) {
		String partnerCode = PartnerCodeHolder.getPartnerCode();
		String sql = getQuerySql();
		sql += " where 1=1 ";
		sql = CommonUtils.addCondition(sql, "b.", null, partnerCode,paramUtils);
		return sqlQueryUtils.queryByCondition(request, sql, MesMoCustDriverLogForm.class);
	}
	
	

	public MesMoCustDriverLogForm getByLbIdAndMoCustExchConfig(String moId,String lbId,String bsCustExchConfigId,ParamUtils paramUtils)  {
		String sql = getQuerySql();
		sql += " where b.mo_id=? ";
		sql += " and b.partner_code=? ";
		sql += " and b.lb_id=? ";
		sql += " and b.bs_cust_exch_config_id=? ";
	    return sqlQueryUtils.get(sql, new Object[] { moId,PartnerCodeHolder.getPartnerCode(),
				lbId, bsCustExchConfigId},MesMoCustDriverLogForm.class);
	}
	
	
	
	/**
	 * 查询单个工单客户交互配置
	 * @param MesMoCustDriverLogForm 查询对象
	 * @param paramUtils 参数对象
	 * @return EditDataResult<MesMoCustDriverLogForm>
	 */
	public EditDataResult<MesMoCustDriverLogForm> getById(MesMoCustDriverLogForm mesMoCustDriverLogForm, ParamUtils paramUtils) {
        // 声明对象
		Locale locale = paramUtils.getLocale();
		LogDto logDto = paramUtils.getLogDto();
		String partnerCode = PartnerCodeHolder.getPartnerCode();
		CheckUtils checkUtils = new CheckUtils(messageSource, parameterService, logDto);
		// 必填
		checkUtils.checkIsEmpty("mesDriverLogId", mesMoCustDriverLogForm.getMesDriverLogId(), locale);
		// 校验数据是否存在
		String sql = getQuerySql();
		sql += " where b.mes_driver_log_id=? "; 
		sql = CommonUtils.addCondition(sql, "b.", null, partnerCode, paramUtils);
		MesMoCustDriverLogForm returnForm = sqlQueryUtils.get(sql, new Object[] { mesMoCustDriverLogForm.getMesDriverLogId() },MesMoCustDriverLogForm.class);
		// 返回
        EditDataResult<MesMoCustDriverLogForm> result = new EditDataResult<MesMoCustDriverLogForm>();
        result.setCount(PangusConstants.RESULT_COUNT_ONE);
        result.setResultCode(PangusConstants.RESTULT_OK);
        result.setData(returnForm);
        return result;
    }
	
	
	/**
	 * 通用检验
	 * @param MesMoCustDriverLogDto 验证对象 
	 * @param paramUtils 参数对象
	 */
    public void commonValidate(MesMoCustDriverLogDto mesMoCustDriverLogDto,ParamUtils paramUtils,String type){
		// 声明对象
		LogDto logDto = paramUtils.getLogDto();
		Locale locale = paramUtils.getLocale();
		CheckUtils checkUtils = paramUtils.getCheckUtils();
		//非空验证
		checkUtils.checkIsEmpty("mesDriverLogId", mesMoCustDriverLogDto.getMesDriverLogId(), locale);
		checkUtils.checkIsEmpty("moId", mesMoCustDriverLogDto.getMoId(), locale);
		checkUtils.checkIsEmpty("bsCustExchConfigId", mesMoCustDriverLogDto.getBsCustExchConfigId(), locale);
		checkUtils.checkIsEmpty("lbId", mesMoCustDriverLogDto.getLbId(), locale);
		if(type=="Add"){
			checkUtils.checkIsEmpty("createdBy", mesMoCustDriverLogDto.getCreatedBy(), locale);
			checkUtils.checkIsEmpty("createdDate", mesMoCustDriverLogDto.getCreatedDate(), locale);
		}
		
		if(type=="Edit"){
			checkUtils.checkIsEmpty("updatedBy", mesMoCustDriverLogDto.getUpdatedBy(), locale);
			checkUtils.checkIsEmpty("updatedDate", mesMoCustDriverLogDto.getUpdatedDate(), locale);
		}
	}
	
	
	
	/**
	 * 新增工单客户交互配置
	 * @param dtoRequest 新增对象
	 * @param paramUtils 参数对象 
	 * @return EditDataResult<MesMoCustDriverLogForm>
	 */
	public EditDataResult<MesMoCustDriverLogForm> add(JsonRequest<MesMoCustDriverLogForm> dtoRequest,ParamUtils paramUtils) {	
		
		// 声明对象
		LogDto logDto = paramUtils.getLogDto();
		Locale locale =paramUtils.getLocale();
		CheckUtils checkUtils = new CheckUtils(messageSource, parameterService, logDto);	
		//报表文件id
		MesMoCustDriverLogForm mesMoCustDriverLogForm = dtoRequest.getForm();
        //新增
		mesMoCustDriverLogForm.setMesDriverLogId(fastIdUtils.nextId());
		mesMoCustDriverLogForm.setCreatedBy(paramUtils.getUserId());
		mesMoCustDriverLogForm.setUpdatedBy(paramUtils.getUserId());
		Date now = new Date();
		mesMoCustDriverLogForm.setCreatedDate(now);
		mesMoCustDriverLogForm.setUpdatedDate(now);
		
        //通用校验
        paramUtils.setCheckUtils(checkUtils);
        commonValidate(mesMoCustDriverLogForm,paramUtils,"Add");
		int count = this.insertNotNull(mesMoCustDriverLogForm);

		//校验返回值
		checkUtils.checkInsertDataIsOk(count, 1, (JSONObject)JSONObject.toJSON(mesMoCustDriverLogForm), MesMoCustDriverLogForm.class);
		//返回
		EditDataResult<MesMoCustDriverLogForm> result = new EditDataResult<MesMoCustDriverLogForm>();
		result.setData(mesMoCustDriverLogForm);
		result.setCount(count);
		result.setResultMsg(MsgUtils.saveMsg(null, messageSource, locale));
		return result;
	}
	
	/**
	 * 修改工单客户交互配置
	 * @param dtoRequest 修改对象
	 * @param paramUtils 参数对象 
	 * @return EditDataResult<MesMoCustDriverLogForm>
	 */
	public EditDataResult<MesMoCustDriverLogForm> edit(JsonRequest<MesMoCustDriverLogForm> dtoRequest,ParamUtils paramUtils) {
		MesMoCustDriverLogForm mesMoCustDriverLogForm = dtoRequest.getForm();
		// 声明对象
		LogDto logDto = paramUtils.getLogDto();
		Locale locale =paramUtils.getLocale();
		CheckUtils checkUtils = new CheckUtils(messageSource, parameterService, logDto);
		//通用校验
		paramUtils.setCheckUtils(checkUtils);
		commonValidate(mesMoCustDriverLogForm, paramUtils,"Edit");
        //验证数据是否不存在
		MesMoCustDriverLogDto mesMoCustDriverLogDto = this.selectByPrimaryKey(mesMoCustDriverLogForm); 
		// 验证数据是否已被修改
		checkUtils.checkIsChanged(mesMoCustDriverLogDto.getUpdatedDate(), mesMoCustDriverLogForm.getUpdatedDate(), locale);	
		mesMoCustDriverLogForm.setUpdatedBy(paramUtils.getUserId());
		mesMoCustDriverLogForm.setUpdatedDate(new Date());
		int count = this.updateByPrimaryKey(mesMoCustDriverLogForm);
		checkUtils.checkUpdateDataIsOk(count, 1, (JSONObject)JSONObject.toJSON(mesMoCustDriverLogForm), MesMoCustDriverLogForm.class);
		//封装返回数据
		EditDataResult<MesMoCustDriverLogForm> result = new EditDataResult<MesMoCustDriverLogForm>();
		result.setCount(count);
		result.setData(mesMoCustDriverLogForm);
		result.setResultMsg(MsgUtils.saveMsg(null, messageSource, locale));
		return result;
	}
	
	
	/**
	 * 删除工单客户交互配置
	 * @param dtoList 删除对象
	 * @param paramUtils 参数对象
	 * @return EditDataResult<MesMoCustDriverLogDto>
	 */
	public EditDataResult<MesMoCustDriverLogDto> delete(List<MesMoCustDriverLogDto> dtoList,ParamUtils paramUtils) {
		// 声明对象
		int count = 0;
		LogDto logDto = paramUtils.getLogDto();
		Locale locale =paramUtils.getLocale();
        CheckUtils checkUtils = new CheckUtils(messageSource, parameterService, logDto);
        // 校验空值
        checkUtils.checkIsEmpty("mesDriverLogId", dtoList, locale);
        for (MesMoCustDriverLogDto dto : dtoList) {
            // 非空验证
            checkUtils.checkIsEmpty("mesDriverLogId", dto.getMesDriverLogId(), locale);
            // 验证数据是否不存在
            MesMoCustDriverLogDto mesMoCustDriverLogDto = this.selectByPrimaryKey(dto);
           	checkUtils.checkIsEmpty("mesDriverLogId", mesMoCustDriverLogDto, locale);
        	int deleteCount = this.deleteByPrimaryKey(mesMoCustDriverLogDto);
            checkUtils.checkDeleteDataIsOk(deleteCount, 1, (JSONObject)JSONObject.toJSON(dto), MesMoCustDriverLogForm.class);
            count = count + deleteCount;
        }
        //返回
        EditDataResult<MesMoCustDriverLogDto> result = new EditDataResult<MesMoCustDriverLogDto>();
        result.setCount(count);
        result.setResultCode(PangusConstants.RESTULT_OK);
        result.setResultMsg(MsgUtils.delMsg(null, messageSource, locale));
        return result;
	}


	@Override
	public void importConvert(int i, List<MesMoCustDriverLogForm> list, ParamUtils paramUtils) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void exportConvert(int j, List<MesMoCustDriverLogForm> list, ParamUtils paramUtils) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<String> importByExcel(int i, List<MesMoCustDriverLogForm> list, ParamUtils paramUtils) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> EditResult verifyData(Map<String, Object> rowData, int i, ParamUtils paramUtils, JsonRequest<T> extend) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CheckExcelResult checkCommonData(Map<Integer, Map<String, Object>> rowDatas,
			Map<Integer, List<String>> errMsgs, ParamUtils paramUtils, JsonRequest<MesMoCustDriverLogForm> extend) {
		// TODO Auto-generated method stub
		return null;
	}

}
